package com.vzaar.examples;

import java.util.ArrayList;
import java.util.List;

import com.vzaar.Vzaar;
import com.vzaar.transport.VzaarTransportFactory;

/**
 * Helper class for examples which checks the common parameters between 
 * all the examples for debugging, authentication and default URL. It 
 * also checks the system environment for these parameters. System
 * parameters are overridden by command line ones.
 * 
 * @author Marc G. Smith
 */
public class ParameterHelper 
{
	///////////////////////////////////////////////////////////////////////////
	// Private Members ////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
	
	private String[] args;
	private String[] remainingArgs;
	private String oauthToken;
	private String oauthSecret;
	private boolean debug;
	private boolean ssl3;
	private String url;
	
	///////////////////////////////////////////////////////////////////////////
	// Public Methods /////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Create a parameter helper instance and process the command line 
	 * arguments.
	 * 
	 * @param args the command line arguments
	 */
	public ParameterHelper(String[] args) {
		this.args = args;
		process();
	}
	
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Create a Vzaar instance based on the authentication, url and debugging
	 * command line arguments or environment variables.
	 * 
	 * @return a Vzaar instance
	 */
	public Vzaar createVzaar() {
		Vzaar vzaar = null;
		
		if(url != null) {
			vzaar = new Vzaar(url, oauthToken, oauthSecret);
		}
		else {
			vzaar = new Vzaar(oauthToken, oauthSecret);
		}
		
		if(debug) {
			vzaar.setDebugStream(System.out);
		}
		
		if(ssl3) {
			vzaar.setSsl3Only();
		}
		
		return vzaar;
	}
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Return any arguments not processed in the order they were submitted.
	 * 
	 * @return the arguments 
	 */
	public String[] getRemainingArgs() {
		return remainingArgs;
	}
	
	///////////////////////////////////////////////////////////////////////////
	// Private Methods ////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * We'll let this throw an {@link ArrayIndexOutOfBoundsException}
	 */
	private void process() {
		checkEnvironment();
		
		List<String> unprocessedArgs = new ArrayList<String>();
		for(int i = 0; i < args.length; ++i) {
			if(args[i].equals("-debug")) {
				debug = true;
			}
			else if(args[i].equals("-ssl3")) {
				ssl3 = true;
			}
			else if(args[i].equals("-url")) {
				url = args[++i];
			}
			else if(args[i].equals("-oatoken")) {
				oauthToken = args[++i];
			}
			else if(args[i].equals("-oasecret")) {
				oauthSecret = args[++i];
			}
			else if(args[i].equals("-transport")) {
				setTransport(args[++i]);
			}
			else {
				unprocessedArgs.add(args[i]);
			}
		}
		
		remainingArgs = unprocessedArgs.toArray(
			new String[unprocessedArgs.size()]);
	}
	
	///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Check the environment variables for default configuration parameters.
	 */
	private void checkEnvironment() {
		url = System.getenv("VZAAR_URL");
		debug = "true".equals(System.getenv("VZAAR_DEBUG"));
		ssl3 = "true".equals(System.getenv("VZAAR_SSL3"));
		oauthToken = System.getenv("VZAAR_OAUTH_TOKEN");
		oauthSecret = System.getenv("VZAAR_OAUTH_SECRET");
		
		String transport = System.getenv("VZAAR_TRANSPORT");
		setTransport(transport);
	}

	///////////////////////////////////////////////////////////////////////////

	/**
	 * Set the default transport factory.
	 * 
	 * @param name the name which can be hc3 for httpclient 3.1 or hc4 for 
	 * 		httpclient 4.0. 
	 */
	private void setTransport(String name) {
		if("hc3".equals(name)) {
			VzaarTransportFactory.setDefaultFactory(
				new com.vzaar.transport.httpclient3.HttpClientTransportFactory());
		}
		else if("hc4".equals(name)) {
			VzaarTransportFactory.setDefaultFactory(
				new com.vzaar.transport.httpclient4.HttpClientTransportFactory());
		}
	}

	///////////////////////////////////////////////////////////////////////////
}
